package com.ratta.supernote.plugin.sticker.lib.view.editText;

import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class ByteLengthFilter implements InputFilter {
    private final String TAG = "ByteLengthFilter";
    private final int mMaxBytes;

    public ByteLengthFilter(int maxBytes) {
        mMaxBytes = maxBytes;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        String currentText = dest.toString();
        String inputStr = source.subSequence(start, end).toString();
        // 获取当前文字开头的长度
        int currentStartLen = currentText.substring(0, dstart).getBytes().length;
        // 获取当前文字结束的长度
        int currentEndLen = currentText.substring(dend).getBytes().length;
        // 获取输入文字的长度
        int inputLen = inputStr.getBytes().length;
        // 粘贴后文字的长度
        int textLen = currentEndLen + currentStartLen + inputLen;
        if (textLen > mMaxBytes) {
            int lenOffset = textLen - mMaxBytes;
            inputLen = inputLen - lenOffset;
            if (inputLen <= 0) {
                return "";
            }
            inputStr = getWholeText(inputStr, inputLen);
            return inputStr;

        }
        return null;

    }

    private String toHexString(String str) {
        StringBuilder hex = new StringBuilder();
        for (char c : str.toCharArray()) {
            hex.append(String.format("%04X ", (int) c));
        }
        return hex.toString();
    }

    private static int getCharWidth(byte[] bytes, int pos) {
        if ((bytes[pos] & 0x80) == 0) return 1;
        if ((bytes[pos] & 0xE0) == 0xC0) return 2;
        if ((bytes[pos] & 0xF0) == 0xE0) return 3;
        return 4;
    }

    private static int countBytes(String str, Charset charset) {
        return str.getBytes(charset).length;
    }

    private String getWholeText(String text, int byteCount) {
        if (text == null || text.isEmpty()) return text;

        byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
        if (bytes.length <= byteCount) return text;

        StringBuilder suText = new StringBuilder();
        int len = text.length();
        for (int offset = 0; offset < len; ) {
            if (offset >= len) {
                break;
            }
            int codePoint = Character.codePointAt(text, offset);
            int charCount = Character.charCount(codePoint);
            // BOM字符不计算
            if (codePoint == 0xFEFF) {
                Log.i(TAG, "has BOM:");
                offset += charCount;
                continue;
            }
            // 确保不会越界
            int endOffset = Math.min(offset + charCount, len);
            if (endOffset > len) {
                endOffset = len;
            }
            String currentStr = text.substring(offset, endOffset);

            int subLen = currentStr.getBytes().length + suText.toString().getBytes().length;
            if (subLen > byteCount) {
                break;
            }

            suText.append(currentStr);
            if (subLen == byteCount) {
                break;
            }

            offset += charCount;
        }
        return suText.toString().toString();
    }
}